/**
 * 
 * @Author: biubiu0002
 * @Date: 2021-04-26 11:41
 **/



#include "CourseSelection.h"
#include "CourseSelectionMapper.h"
#include "list"
#include "iostream"
#include "fstream"
#include "sstream"
#include "string"
#include "vector"

using namespace std;

CourseSelection CourseSelectionMapper::selectByCourseSelectionId(std::string studentId,
                                                       std::string courseId,
                                                       int schoolYear,
                                                       int semester){
    list<CourseSelection> courseSelectionList = getCourseSelectionList();
    vector<CourseSelection> resultList;
    for(CourseSelection courseSelection:courseSelectionList){
        if(courseSelection.getCourseId()==courseId
        &&courseSelection.getStudentId()==studentId
        &&courseSelection.getSchoolYear()==schoolYear
        &&courseSelection.getSemester()==semester){
            resultList.push_back(courseSelection);
        }
    }
    if(resultList.size()==1)
        return resultList.at(0);

    else if(resultList.size()==0){
        cout<<"CourseSelectionMapper没有找到该选课情况"<<endl;
    }
    else{
        cout<<"CourseSelection.txt表中出现选课异常，请检查文件！"<<endl;
    }
    return resultList.at(0);


}

vector<CourseSelection> CourseSelectionMapper::selectByCourseId(string courseId){
    list<CourseSelection> courseSelectionList = getCourseSelectionList();
    vector<CourseSelection> resultList;
    for(CourseSelection courseSelection:courseSelectionList){
        if(courseSelection.getCourseId()==courseId){
            resultList.push_back(courseSelection);
        }
    }
    return resultList;
}

list<CourseSelection> CourseSelectionMapper::loadCourseSelection() {
    list<CourseSelection> courseSelectionList;
    ifstream ifs("../../resource/CourseSelection.txt",ios::in);
    if(!ifs){
        cout<<"CourseSelection.txt文件打开失败"<<endl;
    }
    string line,field;
    while(getline(ifs,line)){
        vector<string> infoList;
        CourseSelection courseSelection;
        istringstream iss(line);
        while(iss>>field){
            infoList.push_back(field);
        }
        courseSelection.setStudentId(infoList[0]);
        courseSelection.setCourseId(infoList[1]);
        courseSelection.setSchoolYear(stoi(infoList[2]));
        courseSelection.setSemester(stoi(infoList[3]));
        infoList[4]=="false"?courseSelection.setIsRelearn(false):courseSelection.setIsRelearn(true);
        courseSelectionList.push_back(courseSelection);

    }
    ifs.close();
    return courseSelectionList;

}

int CourseSelectionMapper::batchInsertCourseSelection(list<CourseSelection> &courseSelectionList) {
    ofstream ofs("../../resource/CourseSelection.txt");
    if(!ofs){
        cout<<"CourseSelection.txt文件打开失败"<<endl;
    }
    for(CourseSelection courseSelection:courseSelectionList){
        ofs<<courseSelection.getStudentId()<<" ";
        ofs<<courseSelection.getCourseId()<<" ";
        ofs<<courseSelection.getSchoolYear()<<" ";
        ofs<<courseSelection.getSemester()<<" ";
        courseSelection.isRelearn()== false?ofs<<"false"<<endl:ofs<<"true"<<endl;
    }
    return 0;
}


vector<CourseSelection>
CourseSelectionMapper::selectByStudentIdAndCourseId(std::string studentId, std::string courseId) {
    vector<CourseSelection> resultList;
    list<CourseSelection> courseSelectionList = this->loadCourseSelection();

    for(CourseSelection courseSelection: courseSelectionList){
        if(courseSelection.getStudentId()==studentId
           && courseSelection.getCourseId()==courseId){
            resultList.push_back(courseSelection);
        }
    }
    return resultList;
}

//换用vector
//std::list<CourseSelection>
//CourseSelectionMapper::selectByStudentIdAndCourseId(string studentId, string courseId) {
//    list<CourseSelection> resultList;
//    list<CourseSelection> courseSelectionList = this->loadCourseSelection();
//
//    for(CourseSelection courseSelection: courseSelectionList){
//        if(courseSelection.getStudentId()==studentId
//           && courseSelection.getCourseId()==courseId){
//            resultList.push_back(courseSelection);
//        }
//    }
//    return resultList;
//}

std::vector<CourseSelection> CourseSelectionMapper::selectByStudentId(std::string studentId) {
    vector<CourseSelection> resultList;
    list<CourseSelection> courseSelectionList =this->getCourseSelectionList();
    for(CourseSelection courseSelection:courseSelectionList){
        if(courseSelection.getStudentId()==studentId){
            resultList.push_back(courseSelection);
        }
    }
    return resultList;
}

const list<CourseSelection> &CourseSelectionMapper::getCourseSelectionList() const {
    return courseSelectionList;
}

void CourseSelectionMapper::setCourseSelectionList(const list<CourseSelection> &courseSelectionList) {
    CourseSelectionMapper::courseSelectionList = courseSelectionList;
}

CourseSelectionMapper::CourseSelectionMapper() {
    this->setCourseSelectionList(this->loadCourseSelection());
}




